package com.yangye.pidoublename.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yangye.pidoublename.service.DoubleNameService;
import com.yangye.pidoublename.service.ProvinceEnum;
import com.yangye.pidoublename.vo.DoubleNameVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * Created by youngyeah on 2019/9/19 0019
 */
@Service
public class ShanDongServiceImpl implements DoubleNameService {

	@Autowired
	private RestTemplate restTemplate;

	private final String COOKIE_URL = "http://www.sdmsjw.gov.cn/vaildatacode/getVaildataCode.do";
	private final String QUERY_URL = "https://www.sdmsjw.gov.cn/populationclient/queryName.shtml";

	@Override
	public DoubleNameVo findByName(String name) {
		MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
		params.add("name", name);
		params.add("type", "count");
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.add("Referer", "https://www.sdmsjw.gov.cn/populationclient/search.shtml");
		httpHeaders.add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		httpHeaders.add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36");
		httpHeaders.add("Cookie", getCookie());
//		httpHeaders.add("Cookie", "__jsluid_h=5780911206596eb1b6814ff301d2b21f; __jsluid_s=92f87143ff3e5dd377139e42f19138ed; Hm_lvt_89140d5b4a1d83b97ff7c72c4b940a1b=1568883489; Hm_lpvt_89140d5b4a1d83b97ff7c72c4b940a1b=1568883513; JSESSIONID=076C84C0920A4ABA4B503F13D5482B58");
		HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(params, httpHeaders);
		JSONObject resultJson = restTemplate.postForObject(QUERY_URL, request, JSONObject.class);
		Integer total = resultJson.getInteger("num");
		DoubleNameVo doubleNameVo = new DoubleNameVo();
		doubleNameVo.setProvince(ProvinceEnum.SHANDONG.getName());
		doubleNameVo.setName(name);
		doubleNameVo.setTotal(total);
		return doubleNameVo;
	}

	private String getCookie() {
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
		httpHeaders.add("Cache-Control", "max-age=0");
		httpHeaders.add("Connection", "keep-alive");
		httpHeaders.add("Host", "www.sdmsjw.gov.cn");
		httpHeaders.add("Sec-Fetch-Mode", "navigate");
		httpHeaders.add("Sec-Fetch-Site", "none");
		httpHeaders.add("Sec-Fetch-User", "?1");
		httpHeaders.add("Upgrade-Insecure-Requests", "1");
		httpHeaders.add("Referer", "https://www.sdmsjw.gov.cn/populationclient/search.shtml");
		httpHeaders.add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36");
		HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(null, httpHeaders);
		ResponseEntity<String> response = restTemplate.postForEntity(COOKIE_URL, request, String.class);
		HttpHeaders headers = response.getHeaders();
		List<String> strings = headers.get("Set-Cookie");
		return strings.get(0);
	}
}
